#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <algorithm>
#include <boost/scoped_ptr.hpp>
#include <cmath>
#include <iostream>

#include "building_type.hpp"
#include "citizen.hpp"
#include "city.hpp"
#include "city_dialog.hpp"
#include "draw_world.hpp"
#include "filesystem.hpp"
#include "font.hpp"
#include "foreach.hpp"
#include "gamemap.hpp"
#include "game_logic.hpp"
#include "government.hpp"
#include "hex_geometry.hpp"
#include "item_type.hpp"
#include "job.hpp"
#include "joystick.hpp"
#include "key.hpp"
#include "mapgen.hpp"
#include "map_display.hpp"
#include "occupation.hpp"
#include "pathfind.hpp"
#include "path_cost_calculators.hpp"
#include "project.hpp"
#include "raster.hpp"
#include "resource.hpp"
#include "sound.hpp"
#include "terrain.hpp"
#include "terrain_improvement.hpp"
#include "tile.hpp"
#include "unit.hpp"
#include "unit_type.hpp"
#include "wml_node.hpp"
#include "wml_parser.hpp"
#include "world.hpp"
#include "world_generator.hpp"

extern "C" int main(int argc, char** argv)
{
	srand(time(NULL));
	const bool fullscreen = false;
	const int width = 1024;
	const int height = 768;

	if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0) {
		std::cerr << "could not init SDL\n";
		return -1;
	}

	if(SDL_SetVideoMode(width,height,0,SDL_OPENGL|(fullscreen ? SDL_FULLSCREEN : 0)) == NULL) {
		std::cerr << "could not set video mode\n";
		return -1;
	}

	{ //manager scope
	const graphics::font::manager font_manager;
	const sound::manager sound_manager;
	const joystick::manager joystick_manager;

	const SDL_Surface* fb = SDL_GetVideoSurface();
	if(fb == NULL) {
		return 0;
	}

	graphics::texture::manager texture_manager;

	glEnable(GL_SMOOTH);
	glEnable(GL_BLEND);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.01);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	hex::terrain::init(wml::parse_wml(sys::read_file("terrain.cfg")));
	building_type::init(wml::parse_wml(sys::read_file("buildings.cfg")));
	item_type::init(wml::parse_wml(sys::read_file("items.cfg")));
	resource::init(wml::parse_wml(sys::read_file("resources.cfg")));
	citizen::init(wml::parse_wml(sys::read_file("citizen.cfg")));
	job_type::init(wml::parse_wml(sys::read_file("jobs.cfg")));
	terrain_improvement::init(wml::parse_wml(sys::read_file("improvements.cfg")));
	craftsman_occupation::init();

	boost::scoped_ptr<world> world(generate_world());

	assert(world->sides().empty() == false);

	//world.add_unit(hex::location(10,10), unit_ptr(new unit(unit_type::get("settler"), world.sides().front(), citizen_ptr(new citizen))));

	map_display(*world, world->sides().front()).play_game();
	}

	SDL_Quit();
	return 0;
}
